﻿onload = function () {
    initSub();
   getRestaurant();
};

var putStar = function(restEl) {

    var name=restEl.firstChild.nextSibling.nextSibling.innerHTML;
	
	alert(name);
    
	ajaxRequest('PUT', encodeURI('http://surface-100062172.appspot.com/restaurant/' + name), {
        'Content-Type': 'application/json;charset=UTF-8'
    }, '{' +'"name":"' + escape(name) + '",'
	       + '"altstar":"' + escape(alt)  + '"'
         
		 + '}', function (status, headers, body) { // success
			    // callback
			    
			    getRestaurant();
			}, function (status, headers, body) { // error callback

			}, null);

};

var initSub = function() {
	
	
	var form = {
			name : "",
			info : ""
		};
	
	var subButton = document.getElementById('subbtn');
	
	subButton.onclick = function() {
		var nameEl = document.getElementById('name').value;
		var infoEl = document.getElementById('info').value;
		
		form.name = nameEl;
		form.info = infoEl;
		
		postRestaurant(form);
	};
	
};

var postRestaurant = function(form) {
	
	ajaxRequest('POST', 'http://surface-100062172.appspot.com/restaurant/' + form.name, {
		'Content-Type' : 'application/json;charset=UTF-8'
	}, '{' +'"name":"' + escape(form.name) + '",' + 
	'"info":"' + escape(form.info)  + '"'
	
			+ '}', function(status, headers, body) { 
             getRestaurant();
		     //alert('餐廳創建成功！');
	}, function(status, headers, body) {
		if(status!=0){
			alert('餐廳已經存在囉！'+status);
			
			}// error callback

	}, null); // run callbacks in global scope
	
};

var getRestaurant = function() {
	
	ajaxRequest('GET', 'http://surface-100062172.appspot.com/restaurant/', {
        'Accept': 'application/json'
    }, null, function (status, headers, body) { // success callback
        // update html
        
        var listEl = document.getElementById('list-area');
        var rests = eval('(' + body + ')');
        if (rests.length == 0) {
            listEl.innerHTML = '<div class="list-no-mail">快餓死了！都沒有開餐廳~~</div>';
            return;
        }
        var htmlFrag = '';
        for (var i = 0; i < rests.length; i++) {
            var htmlFrag = '';
            for (var i = 0; i < rests.length; i++) {
                htmlFrag += '<div id="restinlist">'
                        
							+ '<div class="restaurant">'
							+ '餐廳名稱：'  
							+ '<a class="list-rest-name">'       
							+ escapeHtml(unescape(rests[i].name))
							+'</a>'
							+ '<br />'
							+ '餐廳評價：'
							+ '<a class="list-rest-star">' 
							+ escapeHtml(unescape(rests[i].star))
							+'</a>'
							+ '<br />'
							+ '餐廳簡介：'
							+ '<a class="list-rest-info">' 
							+ escapeHtml(unescape(rests[i].info))
							+'</a>'
							+ '<br />'
							+'<input type="button" value="讚"  >'
							+ '<br />'
							+ '<input type="button" value="刪除餐廳"  >'
							+ '</div>' + '<br />'
						
						+ '</div>';
            }
            listEl.innerHTML = htmlFrag;
			var restEl = listEl.firstChild;
			
			while (restEl) {

            var likeEl = restEl.getElementsByTagName('input')[0];
			var removeEl = restEl.getElementsByTagName('input')[1];
			//alert(removeEl.value);
			
			likeEl.onclick = (function (el) {
                return function () {
				var alt=1;
                    ajaxRequest('PUT', encodeURI('http://surface-100062172.appspot.com/restaurant/' + el.firstChild.firstChild.nextSibling.innerHTML), {
                'Content-Type': 'application/json;charset=UTF-8'
             }, '{' +'"name":"' + escape(el.firstChild.firstChild.nextSibling.innerHTML) + '",'
	       + '"altstar":"' + escape(alt)  + '"'
         
		 + '}', function (status, headers, body) { // success
			    // callback
			    
			    getRestaurant();
			}, function (status, headers, body) { // error callback

			}, null);
                    return false;
                }
            })(restEl);
			
            removeEl.onclick = (function (el) {
                return function () {
                    ajaxRequest('DELETE', encodeURI('http://surface-100062172.appspot.com/restaurant/' + el.firstChild.firstChild.nextSibling.innerHTML)
							, null, null,
							function (status, headers, body) { // success callback
							    getRestaurant();
							}, function (status, headers, body) {  // error callback
							    alert('error: ' + status);
							}, null); // run callbacks in global scope
                    return false;
                }
            })(restEl);
            restEl = restEl.nextSibling;
        }

        }
    }, function (status, headers, body) { // error callback
        alert('error: ' + status);
    }, null);
	
};